#######################################
##     Replicating Tables 3-5        ##
##          Sex Scandals             ##
##   in Hamel and Miller (2018)      ##
#######################################

install.packages(c("Matching", "rgenoud"))
library(Matching)
library(rgenoud)

scandal <- read.csv("scandal_dta.csv")

## All Sex Scandals

sex_scandal <- subset(scandal, sex == 1)

attach(sex_scandal)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_sex <- GenMatch(Tr = scandal_id, X = covariates_match, 
                            BalanceMatrix = covariates_balance, M = 3, 
                            pop.size = 12500, max.generations = 10, wait.generations = 5, 
                            hard.generation.limit = FALSE, int.seed = 132, unif.seed = 844, 
                            fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                            replace = TRUE, ties = FALSE)

effect_sex_vote <- Match(Y = votepct, Tr = scandal_id, 
                               X = covariates_match, M = 3, 
                               exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                               replace = TRUE, ties = FALSE, version = "fast", 
                               Weight.matrix = match_sex)
summary(effect_sex_vote)

effect_sex_win <- Match(Y = win, Tr = scandal_id, 
                          X = covariates_match, M = 3, 
                          exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                          replace = TRUE, ties = FALSE, version = "fast", 
                          Weight.matrix = match_sex, 
                          match.out = effect_sex_vote)
summary(effect_sex_win)

effect_sex_money <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                X = covariates_match, M = 3, 
                                exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                replace = TRUE, ties = FALSE, version = "fast", 
                                Weight.matrix = match_sex, 
                                match.out = effect_sex_vote)
summary(effect_sex_money)

balance_sex <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                    dwnom1 + seniority + les + dpres + 
                                    logged_pre_sum_infl + chall_quality, 
                                  match.out = effect_sex_vote,
                                  nboots = 5000)

detach(sex_scandal)

## Covered Sex Scandals

sex_scandal_covered <- subset(scandal, sex == 1 & covered == 1)

attach(sex_scandal_covered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_sex_covered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                                    BalanceMatrix = covariates_balance, M = 3, 
                                    pop.size = 12500, max.generations = 10, wait.generations = 5, 
                                    hard.generation.limit = FALSE, int.seed = 836, unif.seed = 130, 
                                    fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                    replace = TRUE, ties = FALSE)

effect_sex_vote_covered <- Match(Y = votepct, Tr = scandal_id, 
                                       X = covariates_match, M = 3, 
                                       exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                       replace = TRUE, ties = FALSE, version = "fast", 
                                       Weight.matrix = match_sex_covered)
summary(effect_sex_vote_covered)

effect_sex_win_covered <- Match(Y = win, Tr = scandal_id, 
                                  X = covariates_match, M = 3, 
                                  exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                  replace = TRUE, ties = FALSE, version = "fast", 
                                  Weight.matrix = match_sex_covered, 
                                  match.out = effect_sex_vote_covered)
summary(effect_sex_win_covered)

effect_sex_money_covered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                        X = covariates_match, M = 3, 
                                        exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                        replace = TRUE, ties = FALSE, version = "fast", 
                                        Weight.matrix = match_sex_covered, 
                                        match.out = effect_sex_vote_covered)
summary(effect_sex_money_covered)



balance_sex_covered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                      dwnom1 + seniority + les + dpres + 
                                      logged_pre_sum_infl + chall_quality, 
                                    match.out = effect_sex_vote_covered,
                                    nboots = 5000)
detach(sex_scandal_covered)

## Non-Covered Sex Scandals

sex_scandal_noncovered <- subset(scandal, sex == 1 & covered == 0)

attach(sex_scandal_noncovered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_sex_noncovered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                                       BalanceMatrix = covariates_balance, M = 3, 
                                       pop.size = 12500, max.generations = 10, wait.generations = 5, 
                                       hard.generation.limit = FALSE, int.seed = 840, unif.seed = 489, 
                                       fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                       replace = TRUE, ties = FALSE)

effect_sex_vote_noncovered <- Match(Y = votepct, Tr = scandal_id, 
                                          X = covariates_match, M = 3, 
                                          exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                          replace = TRUE, ties = FALSE, version = "fast", 
                                          Weight.matrix = match_sex_noncovered)
summary(effect_sex_vote_noncovered)


effect_sex_win_noncovered <- Match(Y = win, Tr = scandal_id, 
                                     X = covariates_match, M = 3, 
                                     exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                     replace = TRUE, ties = FALSE, version = "fast", 
                                     Weight.matrix = match_sex_noncovered, 
                                     match.out = effect_sex_vote_noncovered)
summary(effect_sex_win_noncovered)

effect_sex_money_noncovered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                           X = covariates_match, M = 3, 
                                           exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                           replace = TRUE, ties = FALSE, version = "fast", 
                                           Weight.matrix = match_sex_noncovered, 
                                           match.out = effect_sex_vote_noncovered)
summary(effect_sex_money_noncovered)




balance_sex_noncovered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                         dwnom1 + seniority + les + dpres + 
                                         logged_pre_sum_infl + chall_quality, 
                                       match.out = effect_sex_vote_noncovered,
                                       nboots = 5000)

detach(sex_scandal_noncovered)
